﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CCH.DAO;
using CCH.Entity;

public partial class _60_6003_1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            sysfunctionDAO dao = new sysfunctionDAO();

            if (Request.QueryString["mode"] == "new")
            {
                this.lab_title.Text = dao.Get_titleName(6003) + " - 新增";
                this.rbl_type.Items[0].Selected = true;

            }
            else
            {
                this.lab_title.Text = dao.Get_titleName(6003) + " - 修改";
                this.hidd_no.Value = Request.QueryString["ID"];
                int link_no = int.Parse(this.hidd_no.Value);

                link data = new linkDAO().Get_link(link_no);
                this.tbox_name.Text = data.link_name;
                this.tbox_link.Text = data.link_url;
                this.tbox_order.Text = data.link_order.ToString();

                this.rbl_type.DataBind();
                this.rbl_type.Items.FindByValue(data.link_type.ToString()).Selected = true;
            }


        }
    }

    protected void btn_ok_Click(object sender, EventArgs e)
    {
        if (this.Check_Input())
        {
            linkDAO dao = new linkDAO();
            link data;
            string str = "";
            if (this.hidd_no.Value != "")
            {
                data = dao.Get_link(int.Parse(this.hidd_no.Value));
                str = "修改完成!";
            }
            else
            {
                data = new link();
                data.state = "1";
                str = "新增完成!";
            }

            data.link_type = this.rbl_type.SelectedValue;
            data.link_name = this.tbox_name.Text;
            data.link_url = this.tbox_link.Text;
            data.link_order = int.Parse(this.tbox_order.Text);
            data.createtime = DateTime.Now;
            data.createuid = new SessionObject().SessionUserID;

            if (this.hidd_no.Value == "")
            {
                dao.Add_link(data);
            }

            dao.Update();

            JsUtil.UpdateParentJs(this,str);

        }
    }

    private bool Check_Input()
    {
        if (string.IsNullOrEmpty(this.tbox_name.Text))
        {
            JsUtil.AlertJs(this, "請輸入連結名稱!");
            return false;
        }

        if (string.IsNullOrEmpty(this.tbox_link.Text))
        {
            JsUtil.AlertJs(this, "請輸入連結網址!");
            return false;
        }

        if (string.IsNullOrEmpty(this.tbox_order.Text))
        {
            JsUtil.AlertJs(this, "請輸入連結排序!");
            return false;
        }
        else
        {
            try
            {
                int a = int.Parse(this.tbox_order.Text);
            }
            catch
            {
                JsUtil.AlertJs(this, "連結排序需為數字!");
                return false;
            }
        }


        return true;
    }
}